package com.angel.controller;

import com.angel.bean.*;
import com.angel.dto.TemplateDisplayDto;
import com.angel.dto.TemplateDto;
import com.angel.service.*;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/templateEntryController")
public class TemplateEntryController {
    @Resource
    private TemplateService templateService;

    @Resource
    private ProductService productService;

    @Resource
    private AcupuncturePointService acupuncturePointService;
    @Autowired
    private OtherlogService otherlogService;

    @Autowired
    private IllCaseService illCaseService;

    @Autowired
    private VipInfoService vipInfoService;

    /**
     * 获取页面
     *
     * @return templateAddtemp.jsp
     */
    @RequestMapping("/templateEntry")
    public String templateEntry() {
        return "templateAddtemp";
    }

    /**
     * 添加
     *
     * @return 添加模板
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/addTemplate")
    @ResponseBody
    public Object addTemplate(HttpServletRequest request, Template template) {
        //   Template template = new Template();
        Map<Object, Object> map = new HashMap<Object, Object>();
        map.put("result", false);
        template.setState(1);
        int result = templateService.insert(template);
        if (result != 0) {
            map.put("result", true);
        }
        return map;
    }

    /**
     * 查询所有商品
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/findProduct")
    @ResponseBody
    public Object findProduct(Product product) {
        System.out.println("------------------------------------" + product.getProductId());
        Employee et = AdminUserUtils.getPrincipal().getEmployee();
        //通过传回来的病历ID查找病历数据中的用户ID;
        IllCase illCase = new IllCase();
        illCase.setIllCaseId(Integer.valueOf(product.getReserve2()));
        IllCase illCaseTemp = illCaseService.findOne(illCase);
        //通过用户ID查找VIPINFO中的分店ID
        VipInfo vipInfo = new VipInfo();
        vipInfo.setUserId(illCaseTemp.getUserId());
        //通过病历查找用户
        VipInfo resultVIP = vipInfoService.findOne(vipInfo);
        product.setState(1);
        product.setBranId(resultVIP.getBranId());
        product.setReserve2(null);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List<Product> productList = productService.findProduct(product);
        if (productList != null) {
            map.put("productList", productList);
        }
        return map;
    }

    /**
     * 查询所有商品
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/findProducttmp")
    @ResponseBody
    public Object findProductTmp(Product product) {
        Employee et = AdminUserUtils.getPrincipal().getEmployee();
        product.setState(1);
        product.setBranId(et.getBranchesId());
        product.setReserve2(null);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List<Product> productList = productService.findProduct(product);
        if (productList != null) {
            map.put("productList", productList);
        }
        return map;
    }

    /**
     * 查询单个产品
     *
     * @return
     */
    @RequestMapping("/findOneProduct")
    @ResponseBody
    public Object findOneProduct(@RequestParam(value = "proid") int proid) {
        Product product = new Product();
        product.setProductId(proid);
        Product pro = productService.findOne(product);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (pro != null) {
            map.put("pro", pro);
        }
        return map;
    }

    /**
     * 查询单个穴位
     *
     * @return
     */
    @RequestMapping("/findOneAcupuncturePoint")
    @ResponseBody
    public Object findAcupuncturePoint(@RequestParam(value = "acupid") int acupid) {
        AcupuncturePoint acupuncturePoint = new AcupuncturePoint();
        acupuncturePoint.setAcupId(acupid);
        AcupuncturePoint acup = acupuncturePointService.findOne(acupuncturePoint);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (acup != null) {
            map.put("acup", acup);
        }
        return map;
    }

    /**
     * 查询所有穴位
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/findAcupuncturePoint")
    @ResponseBody
    public Object findAcupuncturePoint(AcupuncturePoint acupuncturePoint) {
        acupuncturePoint.setState(1);
        List<AcupuncturePoint> AcupuncturePoint = acupuncturePointService.findAll(null);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (acupuncturePoint != null) {
            map.put("acupList", AcupuncturePoint);
        }
        return map;
    }

    /**
     * 模板搜索
     **/

    @RequestMapping("/searchTemplate")
    @ResponseBody
    public Object searchTemplate(Template template) {
        template.setState(1);
        List<Template> mList = templateService.searchTemplate(template);
        if (mList.size() > 0) {
            return mList;
        }
        return null;
    }


    /**
     * 模板详情页
     *
     * @param model
     * @return
     */
    @RequestMapping("/gettemplate")
    public String getTemplate(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {
            pageNo = 1;
        }
        PageHelper.startPage(pageNo, 10);
        Template template = new Template();
        List<Template> templates = templateService.findTemplate(template);
        List<TemplateDisplayDto> templateDisplayDtos = new ArrayList<>();
        for (Template tt : templates) {
            boolean flag =true;
            TemplateDisplayDto templateDisplayDto = new TemplateDisplayDto(tt);
            JSONArray jsonArray = JSONArray.fromObject(tt.getTemDetail());
            List<TemplateDto> templateDtos = new ArrayList<>();
            //商品模板
            for (Object jsonObject : jsonArray) {
                Map tmp = (Map) jsonObject;
                if ("dietary".equals(tt.getTemType())) {
                    Integer pid = Integer.valueOf(tmp.get("Reid").toString());
                    Product product = new Product();
                    product.setProductId(pid);
                    product = productService.findOne(product);
                    if (product != null){
                        Double price = Double.valueOf(tmp.get("price").toString());
                        TemplateDto templateDto = new TemplateDto(price, Integer.valueOf(tmp.get("productCount").toString()), pid, product.getReserve1());
                        templateDisplayDto.setDto(templateDto);
                    }else {
                        flag =false;
                    }

                } else if ("nurse".equals(tt.getTemType())) {
                    Integer acid = Integer.valueOf(tmp.get("Reid").toString());
                    AcupuncturePoint acupuncturePoint = new AcupuncturePoint();
                    acupuncturePoint.setAcupId(acid);
                    acupuncturePoint = acupuncturePointService.findOne(acupuncturePoint);
                    if (acupuncturePoint != null) {
                        Double price = Double.valueOf(tmp.get("price").toString());
                        TemplateDto templateDto = new TemplateDto(price, Integer.valueOf(tmp.get("productCount").toString()), acid, acupuncturePoint.getAcupName());
                        templateDisplayDto.setDto(templateDto);
                    }else {
                        flag =false;
                    }
                }
            }
            if (flag){
                templateDisplayDtos.add(templateDisplayDto);
            }else {
                templateService.delTempolate(tt.getTemId());
            }

        }
        PageInfo pageInfo = new PageInfo(templateDisplayDtos);
        if (templates != null && !templates.isEmpty()) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "templatemanager";
    }


    @RequestMapping("/getdeils")
    @ResponseBody
    public Map getTemplateDeil(Template template) {
        Map result = new HashMap();
        result.put("success", false);
        template = templateService.findOne(template);
        JSONArray jsonArray = JSONArray.fromObject(template.getTemDetail());
        List<TemplateDto> templateDtos = new ArrayList<>();
        if ("dietary".equals(template.getTemType())) {
            //商品模板
            for (Object jsonObject : jsonArray) {
                Map tmp = (Map) jsonObject;
                Integer pid = Integer.valueOf(tmp.get("Reid").toString());
                Product product = new Product();
                product.setProductId(pid);
                product = productService.findOne(product);
                Double price = Double.valueOf(tmp.get("price").toString());
                TemplateDto templateDto = new TemplateDto(price, Integer.valueOf(tmp.get("productCount").toString()), pid, product.getReserve1());
                templateDtos.add(templateDto);
            }

        } else if ("nurse".equals(template.getTemType())) {
            //穴位
            for (Object jsonObject : jsonArray) {
                Map tmp = (Map) jsonObject;
                Integer acid = Integer.valueOf(tmp.get("Reid").toString());
                AcupuncturePoint acupuncturePoint = new AcupuncturePoint();
                acupuncturePoint.setAcupId(acid);
                acupuncturePoint = acupuncturePointService.findOne(acupuncturePoint);
                Double price = Double.valueOf(tmp.get("price").toString());
                TemplateDto templateDto = new TemplateDto(price, Integer.valueOf(tmp.get("productCount").toString()), acid, acupuncturePoint.getAcupName());
                templateDtos.add(templateDto);
            }

        }
        if (!templateDtos.isEmpty()) {
            result.put("success", true);
            result.put("data", templateDtos);
        } else {
            result.put("msg", "没有查到信息");
        }
        return result;
    }

    @RequestMapping("deltmp")
    @ResponseBody
    public Map delTmplate(@RequestParam("tmid") String tmid) {
        Map result = new HashMap();
        result.put("success", false);
        Integer tid = 0;
        try {
            tid = Integer.valueOf(tmid);
        } catch (Exception e) {
            tid = 0;
        }
        int re = templateService.delTempolate(tid);
        if (re != 0) {
            result.put("success", true);
            result.put("msg", "删除成功");
        } else {
            result.put("msg", "删除失败");
        }

        return result;
    }

}
